<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * C-R-U-D Model
 *
 * @package		 CodeIgniter
 * @subpackage 	 Model
 * @category	 Create-Retrieve-Update-Delete
 *
 * @author		 ihsan hambali / ihsanhambali27[at]gmail.com
 *
 */
 
class Crud extends CI_Model {

	var $data = array();
	var $returnArray = TRUE;
	var $table;
	var $fields;
	var $__numRows;
	var $__insertID;
	var $__affectedRows;
	var $id;
	var $primaryKey = 'id';

	function __construct() {
        parent::__construct();
		$this->load->library('encrypt');
	}

	/**
	 * Load the associated database table.
	 *
	 */
	function useTable($table)
	{
		$this->table  = $table;
		$this->fields = $this->db->list_fields($table);
	}
  

	/**
	 * Returns a resultset array with specified fields from database matching given conditions.
	 */
	function findAll($conditions = NULL, $fields = '*', $order = NULL, $start = 0, $limit = NULL)
	{
		if ($conditions != NULL) {
			$this->db->where($conditions);
		}

		if ($fields != NULL) {
			$this->db->select($fields);
		}

		if ($order != NULL) {
			$this->db->orderby($order);
		}

		if ($limit != NULL) {
			$this->db->limit($limit, $start);
		}

		$query = $this->db->get($this->table);
		$this->__numRows = $query->num_rows();

		return ($this->returnArray) ? $query->result_array() : $query->result();
	}

	/**
	 * Return a single row as a resultset array with specified fields from database matching given conditions.
	 */
	function find($conditions = NULL, $fields = '*', $order = 'id ASC')
	{
		$data = $this->findAll($conditions, $fields, $order, 0, 1);

		if ($data) {
			return $data[0];
		}
		else {
			return false;
		}
	}
	
	function get_insertid(){
		return $this->__insertID;	
	}
	
	function unsetMe(){
		$this->crud->data = array();
		$this->crud->fields = null;
		$this->crud->id = null;
		$this->crud->primaryKey = null;
	}

	/**
	*  Update Method
	*/
	function save($data = null, $id = null, $pk = null, $stat = null)
	{
		if ($data)
		{
			$this->data = $data;
		}

		foreach ($this->data as $key => $value)
		{
			if (array_search($key, $this->fields) === FALSE)
			{
				unset($this->data[$key]);
			}
		}
		
		foreach ($this->data as $key => $value)
		{
			//echo $value;
			if ($value == '')
			{
				unset($this->data[$key]);
			}
		}

		if ($id != null)
		{
			$this->id = $id;
		}
		if ($pk != null)
		{
			$this->primaryKey = $pk;
		}

		$id = $this->id;

		if ($this->id !== null && $this->id !== false)
		{
			$this->db->where($this->primaryKey, $id);
			$ret = $this->db->update($this->table, $this->data);
			$this->__affectedRows = $this->db->affected_rows();
			//if($stat!=99){
			  //echo $this->id;
			  //return $this->id;
			//}
			//echo $this->db->last_query();
		}
		else
		{
			$ret = $this->db->insert($this->table,$this->data);
			$this->__affectedRows = $this->db->affected_rows();
			$this->__insertID = $this->db->insert_id();
			//if($stat!=99){
			  //echo 1;
			  //return $this->__insertID;
			//}
		}
		return $ret;
	}

	/**
	*  Update Method multiple primary key
	*/
	function save_multipk($data = null, $pk = null){
		if ($data){
			$this->data = $data;
		}
		if ($pk){
			$this->pk = $pk;
		}
		   
		$this->db->select('*');
		foreach($this->pk as $key=> $value){
			$this->db->where($key, $value);
		}
		$query = $this->db->get($this->table);
		
		foreach ($this->data as $key => $value){
			if (array_search($key, $this->fields) === FALSE){
				unset($this->data[$key]);
			}
		}
		   
		$numrows = $query->num_rows();
		
		if ($numrows>0){
			foreach($this->pk as $key=> $value){
			   $this->db->where($key, $value);
			}
			$ret = $this->db->update($this->table, $this->data);
			$this->__affectedRows = $this->db->affected_rows();
		}else{ 
			 $ret = $this->db->insert($this->table,$this->data);
			 $this->__affectedRows = $this->db->affected_rows();
			 $this->__insertID = $this->db->insert_id();
			 //return $this->__insertID;
		}
		return $ret;
	}
	
	function delete($id = null, $pk = null)
	{
		if ($id != null)
		{
			$this->id = $id;
		}
		if ($pk != null)
		{
			$this->primaryKey = $pk;
		}

		$id = $this->id;

		if ($this->id !== null && $this->id !== false)
		{
        $this->db->where($this->primaryKey, $id);
        $this->db->delete($this->table);
        $this->__affectedRows = $this->db->affected_rows();
		  //echo $this->id;
        return $this->id;
		}
	}
	
	/**
	*  Get row affected from update
	*/
	function getAffectedRows()
	{
		return $this->__affectedRows;
	}
	
	/**
	*  Get count data using specific condition
	*/
	function getCount($conditions = null)
	{
		$data = $this->findAll($conditions, 'COUNT(*) AS count', null, 0, 1);

		if ($data){
			return $data[0]['count'];
		} else {
			return false;
		}
	}
	
	function get_combo_barang_keluar(){	
		$sql = "select * from barang";
		$query = $this->db->query($sql);
		$html = "";
		foreach($query->result() as $row){
			$html .= "<optgroup label='".$row->idbarang."-".$row->nmbarang."'>";
			$query2 = $this->db->query("select * from barangdetail where idbarang='".$row->idbarang."' and kdstatus<>4");
			foreach($query2->result() as $row2){
				$html .= "<option value='".$row2->idbarangdetail."'>(".$row2->idbarangdetail.")".$row->nmbarang."</option>";
			}
			$html .= "</optgroup>";
		}
		return $html;
	}
	
	function get_combo_supplier($kd=""){	
		$sql = "select * from supplier";
		$query = $this->db->query($sql);
		$html = "";
		foreach($query->result() as $row){
			if($kd==$row->kdsupplier)
				$html .= "<option value='".$row->kdsupplier."' selected>".$row->nmsupplier."</option>";
			else
				$html .= "<option value='".$row->kdsupplier."'>".$row->nmsupplier."</option>";
		}
		return $html;
	}
	
	function get_profil($kd){
		$data = array();
		$options = array('kdprofil' => $kd);
		$query = $this->db->get_where("profil",$options,1);
		if ($query->num_rows() > 0){
			$data = $query->row_array();
		}

		$query->free_result();    
		return $data;
	}
	
	function get_data_row_detail($id){
		$data = array();
		$query = $this->db->query("select a.*,b.nmstatus from barangdetail a inner join status b on a.kdstatus=b.kdstatus where a.idbarangdetail=$id");
		if ($query->num_rows() > 0){
			$data = $query->row_array();
		}

		$query->free_result();    
		return $data;
	}
	
	function get_combo_jenis($kd=""){	
		$sql = "select * from jenis";
		$query = $this->db->query($sql);
		$html = "";
		foreach($query->result() as $row){
			if($kd==$row->kdjenis)
				$html .= "<option value='".$row->kdjenis."' selected>".$row->nmjenis."</option>";
			else
				$html .= "<option value='".$row->kdjenis."'>".$row->nmjenis."</option>";
		}
		return $html;
	}
}
/* End of file crud.php */
/* Location: ./application/model/crud.php */
?>